package com.nari.ysphd.view;

import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.jeremyfeinstein.conference.YCHSConferenceClient;
import com.nari.ysphd.R;
import com.jeremyfeinstein.gson.ChatMessage;
import com.jeremyfeinstein.gson.MeetingUser;

public class ConferenceChatView {
	private static final String TAG = ConferenceChatView.class.getSimpleName();
	private LinearLayout mContainer;
	private Button mChatsend;
	private EditText mChatinput;
	private ListView mChatlist;
	private ChatAdapter mChatAdapter;
	private TextView mChat2user;
	private String toUser;
	private Context mContext;
	private ChatMessage chatMessage;

	public ConferenceChatView(LinearLayout container, Context context) {
		setContainer(container);
		toUser = "all";
		mChat2user = (TextView)container.findViewById(R.id.chat_touser);
		mChatsend = (Button) container.findViewById(R.id.chat_send);
		mChatinput = (EditText) container.findViewById(R.id.chat_input);
		mChatlist = (ListView) container.findViewById(R.id.chat_list);
		this.mContext = context;
		mChatAdapter = new ChatAdapter();
		mChatlist.setAdapter(mChatAdapter);
		mChatsend.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String chatDetail = mChatinput.getText().toString();
				if (!"".equals(chatDetail)) {
					if("all".equals(toUser)){
						YCHSConferenceClient.getInstance().chatMessage(chatDetail, YCHSConferenceClient.SEND_ALL, toUser);
					}else{
						YCHSConferenceClient.getInstance().chatMessage(chatDetail, YCHSConferenceClient.SEND_SINGLE, toUser);
					}
				} else {
					Toast.makeText(mContext, "您不能发送空消息！",
							Toast.LENGTH_SHORT).show();
				}
				mChatinput.setText("");
				refresh();
			}
		});
		
	}
	
	public void setChat2User(MeetingUser user){
		if(user==null){
			mChat2user.setText("@所有人");
			toUser = "all";
		}else{
			mChat2user.setText("@"+user.getName());
			toUser = user.getUserSN();
		}
	}
	
	public void setForbidTalk(){
		mChatinput.setFocusableInTouchMode(false);
		mChatinput.setCursorVisible(false);
		mChatinput.setFocusable(false);
		mChatinput.setEnabled(false);
		mChatsend.setEnabled(false);
	}
	
	public void setAllowTalk(){
		mChatinput.setFocusable(true);
		mChatinput.setCursorVisible(true);
		mChatinput.setFocusableInTouchMode(true);
		mChatinput.requestFocus();
		mChatinput.setEnabled(true);
		mChatsend.setEnabled(true);
	}
	
	
	public TextView getChat2User(){
		return mChat2user;
	}

	public LinearLayout getContainer() {
		return mContainer;
	}

	public void setContainer(LinearLayout container) {
		this.mContainer = container;
	}
	
	public void refresh(){
		mContainer.post(new Runnable() {
			@Override
			public void run() {
				Log.d(TAG, "refresh: "+mChatAdapter.getCount());
				mChatAdapter.notifyDataSetChanged();
			}
		});
	}

	public int getResId(String name) {
		Resources mResources = mContainer.getResources();
		String packageName = mContainer.getContext().getApplicationInfo().packageName;
		return mResources.getIdentifier(name, "id", packageName);
	}

	class ChatAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return YCHSConferenceClient.getInstance().getChatMessages().size();
		}

		@Override
		public Object getItem(int position) {
			return YCHSConferenceClient.getInstance().getChatMessages().get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView==null){
				convertView = View.inflate(getContainer().getContext(), R.layout.item_chart, null);
			}
			ChatMessage chatMessage = (ChatMessage) getItem(position);
			TextView chat_user = (TextView)convertView.findViewById(R.id.chat_user);
			TextView chat_content = (TextView)convertView.findViewById(R.id.chat_content);
			if (YCHSConferenceClient.SEND_ALL.equals(chatMessage.getSendObject())) {
				chat_user.setText(chatMessage.getUserName() + " @所有人:");
			} else {
				if (TextUtils.isEmpty(chatMessage.getToUserCode())) {
					chat_user.setText(chatMessage.getUserName() + " @我");
				} else {
					MeetingUser user = YCHSConferenceClient.getInstance().getUserByUserCode(chatMessage.getToUserCode());
					if (user!=null) {
						chat_user.setText("我  @"+user.getName());
					} else {
						chat_user.setText("我  @");
					}
				}
			}
			chat_content.setText(chatMessage.getMessage());
			return convertView;
		}
	}


	
}
